
//
// 120127 - (AH) - Created.
// 120128 - (AH) - (Chaos *) Finished code.
// 120131 - (AH) - (Huset) Writes XML for factors alone because is possible
//                 factors exist with no products.
// 120201 - (AH) - Encodes using IOUtil.encode;
// 120225 - (AH) - (Chaos) Fixed product names not encoded.
//

package org.soekrat.engine.ontology.ptable;

import static org.soekrat.engine.io.IOUtil.encode;

import java.io.PrintWriter;

import org.soekrat.engine.SoekratName;

/**
 * <p>
 *   Generates XML for 
 *   {@link ProductTable}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class ProductTableXmlGenerator 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ProductTableXmlGenerator(){}
  
  /**
   * <p>
   *   Generates XML.
   * </p>
   * @param pt Object to generate XML 
   * @param pw
   */
  public void perform(Object o, PrintWriter pw)
  {
    if (o==null)
    {
      throw new NullPointerException("o");
    }
    if (pw==null)
    {
      throw new NullPointerException("pw");
    }
    
    ProductTable pt=(ProductTable)o;
    
    StringBuilder sb=new StringBuilder();
    
    pw.println("<product-table>");
    
    if (!pt.isEmpty())
    {  
      pw.println("  <factors>");
      
      int n=pt.getRowFactorCount();
      if (n>0)
      {
        pw.println("    <row-factors>");
        for (int i=0; i<n; i++)
        {
          ProductTableFactor rf=pt.getRowFactor(i);
          String name=rf.getSoekratName().toString();
          sb.setLength(0);
          sb.append("      <factor name='").append(encode(name)).append("' />");
          pw.println(sb.toString());
        }
        pw.println("    </row-factors>");
      }
      
      n=pt.getColFactorCount();
      if (n>0)
      {
        pw.println("  <col-factors>");
        for (int i=0; i<n; i++)
        {
          ProductTableFactor cf=pt.getColFactor(i);
          String name=cf.getSoekratName().toString();
          sb.setLength(0);
          sb.append("      <factor name='").append(encode(name)).append("' />");
          pw.println(sb.toString());
        }
        pw.println("    </col-factors>");
      }
      
      pw.println("  </factors>");
    
      for (Product p : pt) 
      {
        SoekratName rf=p.getRowFactor();
        String rn=rf.toString();
        
        SoekratName cf=p.getColFactor();
        String cn=cf.toString();
      
        String name=p.getSoekratName().toString();
        
        sb.setLength(0);
        sb.append(  "  <product name='").append(encode(name))
          .append("' >");
        pw.println(sb.toString());
      
        sb.setLength(0);
        sb.append("    <row-factor name='").append(encode(rn)).append("' />");
        pw.println(sb.toString());
      
        sb.setLength(0);
        sb.append("    <col-factor name='").append(encode(cn)).append("' />");
        pw.println(sb.toString());
      
        pw.println("  </product>");
      }
    }
    
    pw.println("</product-table>");
  }
  
}
